import { Component, OnInit } from '@angular/core';
import { OrderService } from '@app/services/order/order.service';
import { storage } from '@app/utils';
import { tableId } from '@app/config';

@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.scss']
})
export class OrderComponent implements OnInit {
  private tableId: string = '';
  private countNum: number = 0;
  private countPrice: number = 0;
  private orderList: any[] = []
  constructor (
    private readonly orderService: OrderService,
  ) {
    this.tableId = storage.getItem(tableId);
  }

  ngOnInit() {
    this.initOrderDetails();
  }

  // 获取订单详情
  private initOrderDetails(): void {
    this.orderService.confirmOrderDetails$(this.tableId).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        result.forEach((item: any) => {
          this.countNum += item.num;
          this.countPrice += item.shopPrice * item.num;
        })
        this.orderList = result;
      }
    })
  }
}
